package com.ym.user;

import com.ym.utils.WeixinApi;
import lombok.SneakyThrows;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.boot.web.server.WebServer;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
//import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.net.InetAddress;

@Component
@EnableDiscoveryClient
@EnableFeignClients
@EntityScan("com.ym.*.entity")
@MapperScan(basePackages = {"com.ym.*.dao"})
@ComponentScan(basePackages = "com.ym.*")
//@EnableBinding(Source.class)
@SpringBootApplication  //(exclude = DataSourceAutoConfiguration.class)
//@SpringBootApplication(scanBasePackages="com.ym.*")
public class UserServerApplication implements ApplicationListener<WebServerInitializedEvent> {
    private static final Logger logger = LoggerFactory.getLogger(UserServerApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(UserServerApplication.class, args);
    }

    @SneakyThrows
    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        WebServer server = event.getWebServer();
        WebServerApplicationContext context = event.getApplicationContext();
        Environment env = context.getEnvironment();
        String ip = InetAddress.getLocalHost().getHostAddress();
        int port = server.getPort();
        String contextPath = env.getProperty("server.servlet.context-path");
        if (contextPath == null) {
            contextPath = "";
        }
        String agreement = env.getProperty("server.ssl.key-store");
        if (agreement == null) {
            agreement = "http";
        } else {
            agreement = "https";
        }
        logger.info("\n---------------------------------------------------------\n" +
                "\tApplication is running! Access address:\n" +
                "\tLocal:\t\t {}://localhost:{}" +
                "\n\tExternal:\t {}://{}:{}{}" +
                "\n\tSwagger3 Api Doc:\t{}://{}:{}{}/doc.html#/" +
                "\n---------------------------------------------------------\n", agreement, port, agreement, ip, port, contextPath, agreement, ip, port, contextPath);
    }
}
